//
//  BirthdayItem.m
//  Birthday Reminder
//
//  Created by Andrei Marcu on 10/22/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "BirthdayItem.h"
#import "DateTools.h"

@interface BirthdayItem  (Private)

@end

@implementation BirthdayItem

@synthesize name;
@synthesize birthday;
@synthesize age;
@synthesize remainingDaysUntilBirthday;
@synthesize birthDate;
@synthesize image;
@synthesize notificationDate;
@synthesize recordID;
@synthesize nextBirthday;

- (void)dealloc {
    self.recordID = nil;
    self.name = nil;
    self.notificationDate = nil;
    self.birthDate = nil;
    self.image = nil;
    [super dealloc];
}

- (NSString *)birthday {
    NSDateFormatter *formater = [[[NSDateFormatter alloc] init] autorelease];
    [formater setDateFormat:@"dd MMMM yyyy"];
    return [formater stringFromDate:birthDate];
}

- (NSString *)age {
    if (birthDate == nil) {
        return @"?";
    }
    return [DateTools getNumberOfYearsFromDate:birthDate toDate:[NSDate date]];
}

- (NSString *)remainingDaysUntilBirthday {
    if (birthDate == nil) {
        return @"?";
    }
    NSInteger days = [DateTools getDayComponentFromDate:[NSDate date] toDate:self.nextBirthday];
    NSInteger months = [DateTools getMonthComponentFromDate:[NSDate date] toDate:self.nextBirthday];
    NSString *daysString = nil;
    NSString *monthsString = nil;
    if (days != 0) {
        if (days > 1) {
            daysString = [NSString stringWithFormat:@"%d days", days];
        } else {
            if (months == 0) {
                daysString = @"tomorow";
            } else {
                daysString = [NSString stringWithFormat:@"%d day", days];
            }
        }
    } else {
        if (months == 0) {
            daysString = @"today";
        }
    }
    if (months == 0) {
        return daysString;
    }
    if (months > 1) {
        monthsString = [NSString stringWithFormat:@"%d months", months];
    } else if (months == 1) {
        monthsString = [NSString stringWithFormat:@"%d month", months];
    }
    if (monthsString != nil) {
        if (daysString != nil) {
            return [NSString stringWithFormat:@"%@ and %@", monthsString, daysString];
        } else {
            return monthsString;
        }
    }
    return nil;
}

- (NSDate *)nextBirthday {
    return [DateTools getNextBirthdayForDate:birthDate];
}

@end
